--- 模块功能：串口发送433射频信号
-- @author zhangfenghe
-- @module LOC.send433RFSignal
-- @license MIT
-- @copyright openLuat
-- @release 2022.04.19
module(..., package.seeall)
require "uartTask"
require "nvm"
require "net"
require "logGenerator"
require "earthquakeWaring"
nvm.init("config.lua", true)
InterruptSendMsg, TimeId = false, nil
-- 433射频信号参数初始化
local dateNow, timeNow, red, green, blue, netStatus, broadcast, relay,
      selfCheck, lampFlickerFrequency, sendMsg, logInfo, errInfo, sysTime =
    os.date("%x"), os.date("%c"), "R00", "G00", "B00", 0, "00", 0, "T0", 00, "",
    "", "", ""
-- 获取配置参数
local autoCheckTime, hz, alertSound, enable433, luminance = tonumber(nvm.get(
                                                                         "autoCheckTime")),
                                                            nvm.get("hz"),
                                                            tonumber(
                                                                nvm.get(
                                                                    "alertSound")),
                                                            nvm.get("enable433"),
                                                            tonumber(
                                                                nvm.get(
                                                                    "luminance"))
-- 组装发送参数
local countDownString = ""

-- 获取震级组成色
local function getCompositionColor(warningColor)
    local redColor, greenColor, blueColor = "R00", "G00", "B00"
    if warningColor == 0 then
        if luminance >= 10 then
            redColor, greenColor, blueColor = "R10", "G10", "B10"
        elseif luminance > 0 then
            redColor, greenColor, blueColor = "R0" .. luminance,
                                              "G0" .. luminance,
                                              "B0" .. luminance
        end
    elseif warningColor == 1 then
        redColor, greenColor, blueColor = "R00", "G00", "B10"
    elseif warningColor == 2 then
        redColor, greenColor, blueColor = "R10", "G10", "B00"
    elseif warningColor == 3 then
        redColor, greenColor, blueColor = "R10", "G03", "B00"
    elseif warningColor == 4 then
        redColor, greenColor, blueColor = "R10", "G00", "B00"
    end
    return redColor, greenColor, blueColor
end

-- 烈度转换为阈值
local function intensity2Threshold(intensity)
    local threhold = 0
    if intensity == 1 or intensity == 2 then
        threhold = 1
    elseif intensity == 3 or intensity == 4 then
        threhold = 2
    elseif intensity == 5 or intensity == 6 then
        threhold = 3
    elseif intensity >= 7 then
        threhold = 4
    end
    return threhold
end
function sendMsgTask()
    if "false" == enable433 then enable433 = false end
    if "true" == enable433 then enable433 = true end
    if not InterruptSendMsg and enable433 then
        if TimeId == nil then
            log.info("发送433")
            TimeId = sys.timerLoopStart(signalRF433SerialCommunication, 2000, 0,
                                        0, "0", false)
        end
    end
    if InterruptSendMsg then
        sys.timerStop(TimeId)
        TimeId = nil
    end
end

-- 433射频信号串口通讯
function signalRF433SerialCommunication(earthquakeCountDown, intensity,
                                        alarmSelection, alarmBool)
    dateNow, timeNow = string.format("%02d-%02d-%02d", os.date("*t").year,
                                     os.date("*t").month, os.date("*t").day),
                       string.format("%02d-%02d-%02d", os.date("*t").hour,
                                     os.date("*t").min, os.date("*t").sec)
    red, green, blue = getCompositionColor(intensity2Threshold(intensity))
    if intensity2Threshold(intensity) >= alertSound then
        if earthquakeCountDown > 99 then
            countDownString = tostring(earthquakeCountDown)
        elseif earthquakeCountDown > 10 and earthquakeCountDown <= 99 then
            broadcast = "10"
            countDownString = "0" .. tostring(earthquakeCountDown)
        elseif earthquakeCountDown <= 10 and earthquakeCountDown > 0 then
            broadcast = "11"
            log.info("earthquakeCountDown:", earthquakeCountDown)
            -- audio.play(TTS, "TTS", tostring(earthquakeCountDown), 7)
            if earthquakeCountDown == 10 then
                countDownString = "0" .. tostring(earthquakeCountDown)
            else
                countDownString = "00" .. tostring(earthquakeCountDown)
            end
        elseif earthquakeCountDown == 0 then
            countDownString = "00" .. tostring(earthquakeCountDown)
            broadcast = "00"
        end
    else
        broadcast = "00"
        if earthquakeCountDown > 99 then
            countDownString = tostring(earthquakeCountDown)
        elseif earthquakeCountDown > 10 and earthquakeCountDown <= 99 then
            countDownString = "0" .. tostring(earthquakeCountDown)
        elseif earthquakeCountDown <= 10 and earthquakeCountDown > 0 then
            if earthquakeCountDown == 10 then
                countDownString = "0" .. tostring(earthquakeCountDown)
            else
                countDownString = "00" .. tostring(earthquakeCountDown)
            end
        elseif earthquakeCountDown == 0 then
            countDownString = "00" .. tostring(earthquakeCountDown)
        end
    end
    if net.getNetMode() > 0 then
        netStatus = 0
    elseif net.getNetMode() <= 0 then
        netStatus = 1
    end
    if autoCheckTime > 0 then
        selfCheck = "T1"
    else
        selfCheck = "T0"
    end
    lampFlickerFrequency = hz
    sendMsg = "$" .. string.sub(dateNow, 3, string.len(dateNow)) .. "," ..
                  timeNow .. "," .. red .. "," .. green .. "," .. blue .. "," ..
                  countDownString .. "," .. tostring(netStatus) .. "," .. "0" ..
                  tostring(intensity) .. "," .. broadcast .. "," ..
                  tostring(relay) .. "," .. selfCheck .. "," ..
                  lampFlickerFrequency .. "," .. alarmSelection .. "," ..
                  "CRC16"
    -- log.info("433发送信息：", sendMsg)
    uartTask.write(sendMsg)
    if alarmBool then
        InterruptSendMsg = false
        sendMsgTask()
    end
end

-- 断网发一次
function sendOneMsgTask()
    if enable433 then
        sys.taskInit(signalRF433SerialCommunication, 0, 0, "0", false)
    end
end
